﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SynoManager.DownloadStation.Subscriptions
{
    public partial class SubscriptionSettingsForm : Form
    {
        public SubscriptionSettingsForm()
        {
            InitializeComponent();
            updateIntervalUnitComboBox.SelectedIndex = 0;
        }

        private void autoUpdateCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            updateIntervalNumericUpDown.Enabled =
                updateIntervalUnitComboBox.Enabled =
                autoUpdateCheckBox.Checked;
        }

        private void useRegExRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            regexTextBox.Enabled = useRegExRadioButton.Checked;
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private Subscription subscription = null;
        public Subscription Subscription
        {
            get
            {
                if (subscription == null)
                    subscription = new Subscription();

                subscription.Title = titleTextBox.Text;
                subscription.FeedUri = urlTextBox.Text;

                subscription.AutoUpdate = autoUpdateCheckBox.Checked;
                if (autoUpdateCheckBox.Checked)
                    subscription.UpdateInterval = Interval;

                if (rssLinkRadioButton.Checked)
                    subscription.LinkDiscovery = LinkDiscovery.RssLink;
                else if (this.findTorrentRadioButton.Checked)
                    subscription.LinkDiscovery = LinkDiscovery.DescriptionTorrent;
                else if (this.findNzbRadioButton.Checked)
                    subscription.LinkDiscovery = LinkDiscovery.DescriptionNzb;
                else if (this.useRegExRadioButton.Checked)
                {
                    subscription.LinkDiscovery = LinkDiscovery.Regex;
                    subscription.CustomLinkDiscoveryRegex = regexTextBox.Text;
                }
                return subscription;
            }
            set
            {
                subscription = value;

                titleTextBox.Text = subscription.Title;
                urlTextBox.Text = subscription.FeedUri.ToString();

                autoUpdateCheckBox.Checked = subscription.AutoUpdate;
                if (subscription.AutoUpdate)
                    Interval = subscription.UpdateInterval;

                rssLinkRadioButton.Checked = (subscription.LinkDiscovery == LinkDiscovery.RssLink);
                findTorrentRadioButton.Checked = (subscription.LinkDiscovery == LinkDiscovery.DescriptionTorrent);
                findNzbRadioButton.Checked = (subscription.LinkDiscovery == LinkDiscovery.DescriptionNzb);
                useRegExRadioButton.Checked = (subscription.LinkDiscovery == LinkDiscovery.Regex);
                regexTextBox.Text = subscription.CustomLinkDiscoveryRegex;
            }
        }

        public TimeSpan Interval
        {
            get
            {
                int value = (int) updateIntervalNumericUpDown.Value;
                if (updateIntervalUnitComboBox.SelectedIndex == 0)
                    return new TimeSpan(0, value, 0); // minutes
                else
                    return new TimeSpan(value, 0, 0); // hours
            }
            set
            {
                if (value.TotalMinutes == 0)
                {
                    // Interval is in hours
                    updateIntervalNumericUpDown.Value = (decimal) value.TotalHours;
                    updateIntervalUnitComboBox.SelectedIndex = 1;
                }
                else
                {
                    // Interval is in minutes
                    updateIntervalNumericUpDown.Value = (decimal) value.TotalMinutes;
                    updateIntervalUnitComboBox.SelectedIndex = 0;
                }
            }
        }
    }
}
